package com.jservice.web.sysUser;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;
import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.dto.Message;
import com.jservice.framework.dto.Pager;
import com.jservice.framework.utils.EncryptUtils;
import com.jservice.model.SysUser;
import com.jservice.web.base.JBaseController;
import com.jservice.web.sysOffice.SysOfficeService;
import com.jservice.web.sysRole.SysRoleService;
import org.apache.commons.lang3.StringUtils;

/**
 * UserController
 *
 * @Author cwledit
 * @DATE 2017/5/2.
 */
@RouteBind(path = "/admin/user")
public class UserController extends JBaseController {

    @SuppressWarnings("rawtypes")
	public void list(){
        Pager pager = SysUserService.me().findPage(getPageable());
        setAttr("pager",pager);
        render("list");
    }

    public void checkUsername(){
        String loginName = getPara("sysUser.login_name");
        if (StringUtils.isEmpty(loginName)) {
            renderJson(Boolean.FALSE);
        }
        if (SysUserService.me().usernameExists(loginName)) {
            renderJson(Boolean.FALSE);
        } else {
            renderJson(Boolean.TRUE);
        }

    }

    public void add() {
        setAttr("officeTree", SysOfficeService.me().findTree());
        setAttr("roles", SysRoleService.me().findAll());
        render("add");
    }

    @Before(UserValidator.class)
    public void save(){
        SysUser user = getModel(SysUser.class);
        if (SysUserService.me().usernameExists(user.getLoginName())) {
            render(ERROR_VIEW);
        }
        String[]roleIds = getParaValues("roleIds");

        user.setPassword(EncryptUtils.encryptPassword(user.getPassword(),user.getLoginName().substring(0,3)));
        user.setIsLocked(false);
        user.setLoginFailureCount(0);
        user.setLockedDate(null);
        if(StringUtils.isBlank(user.getName())){
            user.setName(user.getLoginName());
        }
        if(SysUserService.me().save(user,roleIds)){
            setFlashMessage(Message.success("用户添加成功"));
        }else{
            setFlashMessage(Message.error("用户添加失败"));
        }
        redirect("/admin/user/list");
    }

    public void edit() {
        String userId = getPara("id");

        setAttr("roles", SysRoleService.me().findAll());

        Record record = SysUserService.me().findById(userId);

        setAttr("admin", record);
        setAttr("officeTree", SysOfficeService.me().findTree());

        render("edit");
    }

    @Before(UserValidator.class)
    public void update() {
        SysUser user = getModel(SysUser.class);
        String[]roleIds = getParaValues("roleIds");

        if(SysUserService.me().update(user,roleIds)){
            setFlashMessage(Message.success("用户更新成功"));
        }else{
            setFlashMessage(Message.error("用户更新失败"));
        }
        redirect("/admin/user/list");
    }

    public void disable(){
        String[] ids = getParaValues("ids");
        if (ids != null) {
            if(SysUserService.me().disableOrEnabled(ids,Boolean.FALSE)){
                renderJson(Message.success("操作成功"));
            }else{
                renderJson(Message.error("操作失败"));
            }
        }
    }

    public void enable(){
        String[] ids = getParaValues("ids");
        if (ids != null) {
            if(SysUserService.me().disableOrEnabled(ids,Boolean.TRUE)){
                renderJson(Message.success("操作成功"));
            }else{
                renderJson(Message.error("操作失败"));
            }
        }
    }

}
